package org.jpass.dc.view.project;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.jpass.dc.business.project.bean.CProject;
import org.jpass.dc.business.project.logic.ProjectService;
import org.jpass.dc.business.user.bean.UserVOBean;
import org.jpass.dc.common.bean.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GetProjectDetailListAction extends ActionSupport {

	private static final long serialVersionUID = 8992525865208213731L;
	
	private Log log = LogFactory.getLog(getClass());
	
	private ProjectService projectService = new ProjectService();
	
	/* 开始记录数 */
	private int start;
	/* 一页显示的最大记录数 */
	private int limit;
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	@Override
	public String execute() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
				.get(ServletActionContext.HTTP_REQUEST);
		
		UserVOBean user = (UserVOBean) request.getSession().getAttribute("user");
		Page page = new Page(start, limit);
		List<CProject> projectList = projectService.getClosedProjectList(user.getId(), page);
		JSONObject json = new JSONObject();
		json.put("total", page.getTotalCount());
		JSONArray arr = JSONArray.fromObject(projectList);
		json.put("datas", arr);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			log.error(user.getEmail() + "请求项目详细信息获取PrintWriter时抛出异常，异常信息：\n", e);
		}
		return null;
	}

}
